/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import SessionBeans.web.FilesTableSessionBean;
import entities.File;
import entities.FileBlob;
import entities.Folder;
import entities.User;
import filesTableModel.FilesTableModel;
import filesTableModel.TableItemInterface;
import filesTableModel.TableItemTypeEnum;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Tomas
 */
@ManagedBean(name = "filesTableBean")
@ViewScoped
public class FilesTableBean {

    private TableItemInterface selectedItem;
    private TableItemInterface rootItem;
    private FilesTableModel tableModel;
    private String newFolderName;
    private long parentFolderId = -1;
    private File file;
    private User user;

    /**
     * Creates a new instance of FilesTableBean
     */
    public FilesTableBean() {
        tableModel = new FilesTableModel();
    }

    public List<TableItemInterface> getFileList() {
        return rootItem.getChilds();
    }

    public TableItemInterface getRootItem() {
        return rootItem;
    }

    public void setRootItem(TableItemInterface rootItem) {
        this.rootItem = rootItem;
    }

    public TableItemInterface getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(TableItemInterface selectedItem) {
        if (selectedItem != null) {
            this.selectedItem = selectedItem;
        }
    }

    @PostConstruct
    private void initFiles() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        user = (User) session.getAttribute("user");
        rootItem = tableModel.loadUserFile(user);
    }

    public void onRowSelect() {
        if (selectedItem != null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedItem.getName());

            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    public void openFolder() {
        if (selectedItem.getType() == TableItemTypeEnum.FOLDER) {
            
            Folder folder = (Folder) selectedItem.getValue();
            parentFolderId = ((Folder) rootItem.getValue()).getId();
            rootItem = tableModel.loadFiles(folder, user.getId());
            selectedItem = null;
        }
    }

    public void upFolder() {
        rootItem = tableModel.loadFiles(parentFolderId, user.getId());
        Folder parent = ((Folder) rootItem.getValue()).getParentFolder();
        if (parent != null) {
            parentFolderId = parent.getId();
        } else {
            parentFolderId = -5;
        }
        selectedItem = null;
    }

    public boolean isUpMetod() {
        return parentFolderId > -2;
    }

    public void createNewFolder() {

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        User user = (User) session.getAttribute("user");

        rootItem = tableModel.createFolder(user, (Folder) rootItem.getValue(), newFolderName);
    }

    public void handleFileUpload(FileUploadEvent event) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        User user = (User) session.getAttribute("user");

        rootItem = tableModel.createFile(user, (Folder) rootItem.getValue(), event.getFile().getFileName(), event.getFile().getContents());

    }

    public StreamedContent fileDownload() {
        if (selectedItem.getType() == TableItemTypeEnum.FILE) {
            FileBlob loadFileValue = tableModel.loadFileValue((File) selectedItem.getValue());
            InputStream stream = new ByteArrayInputStream(loadFileValue.getValue());
            //InputStream stream = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");
            return new DefaultStreamedContent(stream, null, selectedItem.getName());
        }
        return null;
    }

    public String getNewFolderName() {
        return newFolderName;
    }

    public void setNewFolderName(String newFolderName) {
        this.newFolderName = newFolderName;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String shareFile() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("selectedNode", selectedItem);

        return "sharing.xhtml?faces-redirect=true";
    }

    public boolean isShare() {
        return rootItem.isShared();
    }

    public boolean isSelectedRow() {
        return selectedItem != null;
    }

    public boolean isUploadFolderCreatePermit() {
        return rootItem.getRigts().getTypeCode() > 10;
    }

    public boolean isDeletePermit() {
        if(isSelectedRow()){ 
            return selectedItem.getRigts().getTypeCode() > 1000;
        }
        return false;
    }

    public boolean isSharingPermit() {
        return !rootItem.isShared();
    }
}
